﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.Web;
using System.IO;
using System.Web.Caching;
using nwrole.toolbox.sync.entity;

namespace nhEntity_web.Util
{
    public class MyVirtualPathProvider : VirtualPathProvider
    {
        // virtualPath example >>  "~/Views/View/21EC2020-3AEA-1069-A2DD-08002B30309D.cshtml"
        private bool IsPathVirtual(string virtualPath)
        {
            string path = VirtualPathUtility.ToAppRelative(virtualPath);

            // returns true if the path is requested by the "ViewController" controller
            if (path.StartsWith("~/Views/Virtual", StringComparison.InvariantCultureIgnoreCase))
            {
                return true;
            }
            return false;
        }

        public override bool FileExists(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                SimpleVirtualFile file = (SimpleVirtualFile)GetFile(virtualPath);
                //if (file.Exists)
                //{
                //    return true;
                //}
                return true;
            }
            return Previous.FileExists(virtualPath);
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                return new SimpleVirtualFile(virtualPath);
            }
            return Previous.GetFile(virtualPath);
        }

        // Simply return the virtualPath on every request.
        public override string GetFileHash(string virtualPath, System.Collections.IEnumerable virtualPathDependencies)
        {
            if (IsPathVirtual(virtualPath))
            {
                // Returns the virtual path value which is made up of the views GUID value that only
                // changes once the view has been updated - essentially working like an updated FileHash
                // but also the ID of the requested view
                return virtualPath;
            }
            return base.GetFileHash(virtualPath, virtualPathDependencies);
        }

        private class SimpleVirtualFile : VirtualFile
        {
            private string virtualPath;


            public SimpleVirtualFile(string virtualPath)
                : base(virtualPath)
            {
                this.virtualPath = virtualPath;
            }

            public override Stream Open()
            {

                int pos1 = virtualPath.LastIndexOf("/");
                int pos2 = virtualPath.LastIndexOf(".");
                pos1 = pos1 + 1;
                string str = virtualPath.Substring(pos1, pos2 - pos1);
                // long id = int.Parse(str);

                var viewEntity = MyVirtualPathProvider.fake(); // ViewEntity.FindById(id);


                ASCIIEncoding encoding = new ASCIIEncoding();
                return new MemoryStream(encoding.GetBytes(viewEntity.Content), false);
            }

        }

        // just implement the default override and return null if the path is a Virtual Path
        public override CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            if (IsPathVirtual(virtualPath))
            {
                return null;
            }
            return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }

        public static nhEntityView fake()
        {
            nhEntityView ve = new nhEntityView()
            {
                Model = "Model",
                Name = "k2"
            };

            List<nhEntityViewColumn> list = new List<nhEntityViewColumn>();
            list.Add(new nhEntityViewColumn()
            {
                BindingType=nhEntityViewColumn.BindingTypeEnum.Bound,
                Name = "Namn",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Template,
                Template = @"k => k.StartadeSenast.ToString(""yyyy-MM-dd HH:mm:ss"")",
                Title = "Startade senast"
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Template,
                Template = @"k => k.SlutadeSenast.ToString(""yyyy-MM-dd HH:mm:ss"")",
                Title = "Slutade senast",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Bound,
                Name = "Resultat",
                Title = "Resultatt",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Bound,
                Name = "Logg",
                Title = "Logg",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Bound,
                Name = "Server",
                Title = "Server",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Bound,
                Name = "TvingadStart",
                Title = "Tvingad start",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Bound,
                Name = "LogLevel",
                Title = "Loggnivå",
                HtmlAttributes = @"style = ""border-right:none;"""
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Template,
                Template = @"k => {%> <a href=""<%= Url.RouteUrl(""administration_forceintegration"", new { id = k.Id }) %>"" class=""small-button green""><span>Tvinga</span></a><%}",
            });

            list.Add(new nhEntityViewColumn()
            {
                BindingType = nhEntityViewColumn.BindingTypeEnum.Template,
                Template = @"k => {%> <a href=""<%= Url.RouteUrl(""administration_changeloglevel"", new { id = k.Id }) %>"" class=""small-button green""><span>Loggnivå</span></a><%}",
            });

            ve.Columns = list;
            return ve;
        }

    }

}


/*

 * using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
 * 
            CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
            string Output = "Out.exe";
            CompilerParameters cm = new CompilerParameters();

            // Dim ASM As System.Reflection.Assembly
        // For Each ASM In AppDomain.CurrentDomain.GetAssemblies()

            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
                cm.ReferencedAssemblies.Add(a.Location);
            }
            cm.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);

            var result = codeProvider.CompileAssemblyFromSource(cm, myCode);

            var plopp = (myBase)result.CompiledAssembly.CreateInstance("hej", false);

            plopp.test();

            int a22 = 1;

        }

        static string myCode = @"
public class hej : ConsoleApplication1.myBase {
    public override void test() {
        System.Console.WriteLine(""hejsan"");
    }
}
";
    }
}


*/